package com.evoupsight.monitorpass.gateway.cfg;

import com.evoupsight.monitorpass.gateway.services.SnmpPollerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * @author evoup
 */
@Configuration
@EnableScheduling
public class SnmpScheduleConfigurer implements SchedulingConfigurer {
    @Autowired
    private SnmpPollerService snmpPollerService;
    @Value("${task.snmp_poll.enable:true}")
    private boolean snmpPollEnable;

    @Bean()
    public ThreadPoolTaskScheduler taskSchedulerSnmp() {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(1);
        threadPoolTaskScheduler.initialize();
        return threadPoolTaskScheduler;
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        if (!snmpPollEnable) {
            return;
        }
        taskRegistrar.setTaskScheduler(taskSchedulerSnmp());
        taskRegistrar.addFixedDelayTask(() -> snmpPollerService.poll(), 10000);
    }

}
